const app = getApp();
var sourceType = [
    ['album']
];
Page({
    data: {
        value: '',
        imageList: [],
        sourceTypeIndex: 1,
        sourceType: ['相册'],
        countIndex: 0,
        count: [1]
    },
    // onTextBlur: function(e) {
    //     console.log(e.detail.value);
    // },
    onTextInput: function(e) { // textarea输入
        console.log(e.detail.value);
        this.setData({
            value: e.detail.value,
        });

    },
    handleInput(e) {
        console.log(e.detail.value);
        this.setData({
            number: e.detail.value,
        });
    },
    submit() {
        console.log(this.data.value, this.data.number, this.data.imageList[0]);
        // console.log();
        if (this.data.value == '' || this.data.value == 'undefined') {
            this.showMsg('请输入您遇到的问题或对我们的建议');
        } else if (this.data.number == null || this.data.number == 'undefined') {
            this.showMsg('请留下您的联系方式，我们会尽快为您解决');
        } else {
            tt.request({
                url: `${app.globalData.requestUrl}/dy_mini/check_content`,
                method: 'POST',
                data: {
                    appId: app.globalData.appId,
                    content: this.data.value
                },
                header: {
                    'content-type': 'application/json',
                },
                success: (res) => {
                    console.log(res.data.data);
                    if (res.data.data == true) {
                        tt.showToast({
                            title: "涉及敏感词",
                            icon: "loading",
                            during: 1500
                        })
                        return
                    } else {
                        let token = tt.getStorageSync('access_token');
                        console.log(token);
                        // 图片先上传到服务器再检测图片
                        // 上传图片到服务器
                        tt.uploadFile({
                            url: `${app.globalData.requestUrl}/baseMethod/upload_file`,
                            filePath: this.data.imageList[0],
                            name: 'file', // HTTP请求的文件名
                            success: (res) => {
                                console.log("上传至服务器成功", JSON.parse(res.data));
                                let data = JSON.parse(res.data);
                                console.log(JSON.parse(data.data).files);
                                let imageUrl = JSON.parse(data.data).files[0].url;
                                console.log(imageUrl);
                                // 检测图片敏感性
                                tt.request({
                                    url: `${app.globalData.requestUrl}/dy_mini/check_photo`,
                                    method: 'POST',
                                    data: {
                                        appId: app.globalData.appId,
                                        imageUrl: imageUrl,
                                    },
                                    header: {
                                        'content-type': 'application/json',
                                    },
                                    success: (res) => {
                                        console.log(res.data);
                                        // 成功调用接口储存内容
                                        tt.request({
                                            url: `${app.globalData.requestUrl}/dy_mini/save_feedback_for_mini`,
                                            method: 'POST',
                                            header: {
                                                'content-type': 'application/json',
                                            },
                                            data: {
                                                appId: app.globalData.appId,
                                                imageUrl: imageUrl,
                                                number: this.data.number,
                                                content: this.data.value,
                                            },
                                            success: (res) => {
                                                this.showMsg('已提交,谢谢您的反馈');
                                                // setTimeout(() => {
                                                tt.reLaunch({
                                                    url: '/pages/home/home' // 指定页面的url
                                                });
                                                // }, 50);
                                            }
                                        });

                                    },
                                });
                            },
                            fail: (err) => {
                                console.log("上传失败", err);
                            },
                        });
                    }
                },
                fail: (err) => {
                    console.log(err);
                }
            });
        }
    },
    chooseImage: function() {
        var that = this;
        tt.chooseImage({
            sourceType: ['album'],
            count: 1,
            success(res) {
                that.setData({
                    imageList: res.tempFilePaths
                });
            },
            fail(res) {
                tt.showToast({
                    title: res.errMsg
                });
            },
        });
    },
    previewImage: function(e) {
        console.log(e.target.dataset);
        var current = e.target.dataset.src;
        tt.previewImage({
            current: current,
            urls: this.data.imageList
        });
    },
    clearImage() {
        this.setData({
            imageList: []
        });
    },
    showMsg(n) { // 轻提示方法
        tt.showToast({
            title: n,
            icon: "none"
        });
    },
});